// pages/evaluation/start.js
var util = require("../../../../common/util/util");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    evaluation_id: 0,
    evaluation: null,
    second_flag: 0,//是否是二次测评
    evaluation_latest: {
      create_time_text: "无"
    },
    hasUserInfo: false,
    key: 'none',
    xInfoNeed: false,
    statusBar:'',
    customBar:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    console.log(options)
    this.xGetDoc()
    var evaluation_id = options.id;
    this.setData({
      'options.evaluation_id': options.id,
      evaluation_id: options.id,
      key: options.key,
      week: options.week
    });
    wx.getSystemInfo({
      success: (e) => {
        let custom = wx.getMenuButtonBoundingClientRect();
        this.setData({
          statusBar: e.statusBarHeight,
          customBar: custom.bottom + custom.top - e.statusBarHeight
        })
      }
    })
    // let h = wx.getMenuButtonBoundingClientRect();
    // this.setData({
    //   menuH: "position:fiex;top:"+h.top+"px;left:3vw;"
    // });
    // var second_flag = options.second_flag;
    // if(second_flag == undefined){
    //   second_flag = 0;
    // }
    console.log(this.data.options, this.data.evaluation_id)

    let that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.cloud.callFunction({
      name: 'scale_manage',
      data: {
        action: "get_scale_info",
        return_data_type: 3,
        scale_number: options.id
      }
    }).then(res => {
      console.log(res.result)
      that.setData({
        evaluation: res.result,
        evaluation_id: options.id
      })
    })
    // if(options.key === 'other'){

    // }else{
    //   var res = await wx.cloud.callFunction({
    //     name: "evaluation",
    //     data: {
    //       action: "detail",
    //       id: evaluation_id
    //     }
    //   })
    //   if(await app.autologin()){
    //     that.setData({
    //       userInfo: app.globalData.userInfo,
    //       hasUserInfo: true,
    //     })
    //   }
    //   that.setData({
    //     evaluation: res.result,
    //     evaluation_id: res.result._id
    //   })
    //   if (app.globalData.userInfo) {//若已登陆
    //     console.log(app.globalData.userInfo)
    //     that.setData({
    //       hasUserInfo:true
    //     });
    //     await that.getData();
    //   }
    // }


    wx.hideLoading();
  },
  getData: async function () {
    var that = this;
    //找到该用户最近一次测评时间
    var res = await wx.cloud.callFunction({
      name: "evaluation",
      data: {
        action: "user_latest",
        evaluation_id: that.data.evaluation_id
      }
    })
    if (res.result != null) {
      var evaluation_latest = res.result;
      evaluation_latest.create_time_text = util.formatDate_chinese(new Date(evaluation_latest.create_time));
      that.setData({
        evaluation_latest: evaluation_latest
      })
      //判断上次测评是否是同一个自然日
      var tmpNOW = new Date();
      var latest_create_time = new Date(evaluation_latest.create_time);
      if (tmpNOW.getFullYear() == latest_create_time.getFullYear() && tmpNOW.getMonth() == latest_create_time.getMonth() && tmpNOW.getDate() == latest_create_time.getDate()) {//判断上次测评是否是同一个自然日
        that.setData({
          second_flag: 1
        })
      }
    }
  },
  authorizeLogin: async function (e) {
    var that = this;
    app.authorizeLogin(e, async function () {
      that.setData({
        hasUserInfo: true
      });
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      await that.getData();
      that.startbtnFunc();
      wx.hideLoading();
    });
  },
  startbtnFunc: function () {
    var _this = this;
    // if (_this.data.is_inremind_time == -1){//表示在提醒期限前，则弹出对话框
    //   var content = '建议您在上次测评后两周再次进行测评';
    //   if (_this.data.evaluation.is_depressed){
    //     content = '建议您在预产期后六周进行测评';
    //   }
    //   wx.showModal({
    //     title: '测评提醒',
    //     content: content,
    //     confirmText: "继续测评",
    //     cancelText: "确定",
    //     success(res) {
    //       if (res.confirm) {
    //         //是否是二次测评
    //         var second_flag = _this.data.second_flag
    //         if (second_flag == 1) {
    //           wx.showModal({
    //             title: '再次测评',
    //             content: '建议每个测试每日只做一次，继续测评将会替换掉上一次的测评结果',
    //             success(res2) {
    //               if (res2.confirm) {//用户点击确定
    //                 wx.navigateTo({
    //                   url: '../evaluation/index?id=' + _this.data.evaluation_id + "&second_flag=" + second_flag,
    //                 })
    //               } else if (res2.cancel) {//用户点击取消
    //                 wx.showToast({
    //                   title: '取消再次测评',
    //                   icon: "none"
    //                 })
    //               }
    //             }
    //           })  
    //         }else{
    //           wx.navigateTo({
    //             url: '../evaluation/index?id=' + _this.data.evaluation_id + "&second_flag=" + second_flag,
    //           })
    //         }
    //       } else if (res.cancel) {
    //         console.log('用户点击取消')
    //       }
    //     }
    //   })
    // }else{
    //是否是二次测评
    var second_flag = _this.data.second_flag
    console.log(second_flag)
    if (second_flag == 1) {
      wx.showModal({
        title: '再次测评',
        content: '建议每个测试每日只做一次，继续测评将会替换掉上一次的测评结果',
        success(res) {
          if (res.confirm) { //用户点击确定
            wx.navigateTo({
              url: '/subtool/pages/evaluate/eva/index?id=' + _this.data.evaluation_id + "&second_flag=" + second_flag + '&week=' + _this.data.week + '&key=other',
            })
          } else if (res.cancel) { //用户点击取消
            wx.showToast({
              title: '取消再次测评',
              icon: "none"
            })
          }
        }
      })
    } else {
      let key = _this.data.key
      if (key === 'other') {
        wx.navigateTo({
          url: '/subtool/pages/evaluate/eva/index?id=' + _this.data.evaluation_id + "&second_flag=" + second_flag + "&plan_user_id=" + _this.data.options.plan_user_id + "&key=" + key
        })
      } else {
        if (_this.data.options.plan_user_id && _this.data.options.code) {
          wx.navigateTo({
            url: '/subtool/pages/evaluate/eva/index?id=' + _this.data.evaluation_id + "&second_flag=" + second_flag + "&plan_user_id=" + _this.data.options.plan_user_id + "&code=" + _this.data.options.code,
          })
        } else {
          wx.navigateTo({
            url: '/subtool/pages/evaluate/eva/index?id=' + _this.data.evaluation_id + "&second_flag=" + second_flag,
          })
        }
      }
    }
    // }

  },


  onShareAppMessage: function (event) {
    let that = this
    let eva = that.data.evaluation
    var shareObj = {
      title: eva.name,
      path: '/subtool/pages/evaluate/eva-tip/index?id=' + eva.scale_number,
      imageUrl: '',
    }
    if (event.from == 'button') {
      var eData = event.target.dataset;
      console.log(eData.name);
      shareObj.path = '/subtool/pages/evaluate/eva-tip/index?id=' + eva.scale_number;
    }
    return shareObj;
  },

  xGetDoc: function () {
    let that = this
    wx.cloud.callFunction({
      name: 'user',
      data: {
        action: 'detail'
      }
    }).then(res => {
      let user = res.result
      let key = [
        'nickName'
      ]
      for (var i = 0; i < key.length; i++) {
        if (user[key[i]] == '' || user[key[i]] == undefined) {
          that.setData({
            xInfoNeed: true
          })
          return false
        }else{
          that.setData({
            xInfoNeed: false
          })
        }
      }
    })
  },
  toback: function(){
    wx.navigateBack({
      
    })
  }
})